import { createLocalVue, mount } from '@vue/test-utils';
import Vuex, { ActionTree, GetterTree, Store } from 'vuex';
import {{ properCase componentName }} from './{{ properCase componentName }}.vue';
import { {{ properCase componentName }}Getters, I{{ properCase componentName }}Getters } from '../getters';
import { {{ properCase componentName }}DefaultState, I{{ properCase componentName }}State } from '../state';
import { {{ properCase componentName }}Actions, I{{ properCase componentName }}Actions } from '../actions';
import { i18n } from '@/app/shared/plugins/i18n/i18n';

const localVue = createLocalVue();

localVue.use(Vuex);

describe('{{ properCase componentName }}.vue', () => {
  let store: Store<I{{ properCase componentName }}State>;
  let getters: GetterTree<I{{ properCase componentName }}State, I{{ properCase componentName }}Getters>;
  let actions: ActionTree<I{{ properCase componentName }}State, I{{ properCase componentName }}Actions>;
  let state: I{{ properCase componentName }}State;

  beforeEach(() => {
    getters = {
      ...{{ properCase componentName }}Getters,
    };
    actions = {
      ...{{ properCase componentName }}Actions,
      increment: jest.fn(),
      decrement: jest.fn(),
    };
    state = {
      ...{{ properCase componentName }}DefaultState(),
    };

    store = new Vuex.Store({
                             modules: {
                               {{ camelCase moduleName }}: {
                                 namespaced: true,
                                 getters,
                                 actions,
                                 state,
                               },
                             },
                           } as any);
  });

  test('renders component', () => {
    const wrapper = mount<any>({{ properCase componentName }}, {
      store,
      localVue,
      i18n,
    });

    expect(wrapper.find('h1').text()).toBe('{{ properCase componentName }}');
  });

  test('should increment and decrement', () => {
    const wrapper: any = mount<any>({{ properCase componentName }}, {
      store,
      localVue,
      i18n,
    });

    wrapper.vm.increment();
    expect(actions.increment).toHaveBeenCalled();

    wrapper.vm.decrement();
    expect(actions.decrement).toHaveBeenCalled();
  });

  test('dispatches action on the server', () => {
    store.dispatch = jest.fn();

    {{ properCase componentName }}.prefetch({ store });

    expect(store.dispatch).toHaveBeenCalled();
    expect(store.dispatch).toHaveBeenCalledWith(`{{ camelCase moduleName }}/increment`);
  });

});
